package com.example.photography.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.photography.bean.UserBean;
import com.example.photography.db.UserDB;


//用户信息的操作类
public class UsersTableDao {
    private  final UserDB mHelper;
    public UsersTableDao(Context context) {
            mHelper = new UserDB(context);
        }
        //添加用户到数据库
        public void addAccount(UserBean.User user){
            //获取数据库对象
            SQLiteDatabase db = mHelper.getReadableDatabase();

            // 执行添加操作
            ContentValues values = new ContentValues();
            values.put(UsersTable.id_unique,user.getId_unique());
            values.put(UsersTable.phone, user.getPhone());
            values.put(UsersTable.password,user.getPassword());
            db.replace(UsersTable.TAB_NAME,null,values);
    }



    //根据id_unique获取所有用户信息
    public UserBean.User getAccountById_unique(String id_unique){
        //获取数据库对象
        SQLiteDatabase db = mHelper.getReadableDatabase();

        //执行查询语句
        String sql = "select * from " + UsersTable.TAB_NAME+" where "+ UsersTable.id_unique+ " =?";
        Cursor cursor = db.rawQuery(sql,new String[]{id_unique});
        UserBean.User user = null;
        if (cursor.moveToNext()){
            user = new UserBean.User();
            //封装对象
            user.setId_unique(cursor.getString(cursor.getColumnIndex(UsersTable.id_unique)));
            user.setPhone(cursor.getString(cursor.getColumnIndex(UsersTable.phone)));
            user.setPassword(cursor.getString(cursor.getColumnIndex(UsersTable.password)));
        }
        //关闭资源
        cursor.close();
        //返回数据
        return user;
    }


}
